package com.analysis

//商品的平均单价与销量的关系

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

object ProductPriceVsSales {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("Product Price vs. Sales")
      .master("local[*]")
      .getOrCreate()

    val cleanedDF = spark.read.parquet("output/cleaned_data.parquet")

    val resultDF = cleanedDF
      .filter(col("Description").isNotNull and col("Quantity") > 0)
      .groupBy("Description", "StockCode")
      .agg(
        avg("UnitPrice").as("AveragePrice"),
        sum("Quantity").as("TotalQuantity")
      )
      .orderBy(desc("TotalQuantity"))
      .limit(1000) // 限制结果数量，防止文件过大

    resultDF.write
      .mode("overwrite")
      .json("output/analysis_results/product_price_vs_sales")

    resultDF.show()

    spark.stop()
  }
}